
package com.work.mvc.controler;

import java.util.*;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.*;
import com.work.WorkovertimeApplication;
import com.work.db.entity.LeaveTypeEnum;
import com.work.mvc.*;
import com.work.mvc.view.panels.reports.*;
import com.work.mvc.view.window.ReportWindowView;
import com.work.reports.*;

public class ReportsController implements Controller, ClickListener, ValueChangeListener {
	
	public static final String CRITERIA_LEAVE_TYPES = "leaveTypes";

	private static final long serialVersionUID = -8659148312171681205L;

	private ReportPanel view;
	
	private Map<String, Object> criteria;

	public ReportsController(ReportPanel view) {
		this.view = view;
		this.criteria = new HashMap<String, Object>();
	}

	@Override
	public View getView()
	{
		return null;
	}

	@Override
	public void buttonClick(ClickEvent event)
	{
		ReportType reportType = (ReportType) event.getButton().getData();

		if (reportType != null) {
			PdfReport reportGenerator = ReportsFacade.instance().getReportGenerator(reportType, criteria);

			WorkovertimeApplication application = (WorkovertimeApplication) UI.getCurrent();
			application.addWindow(new ReportWindowView(reportType, reportGenerator));
		}
	}

	@Override
	public void valueChange(ValueChangeEvent event)
	{
		criteria.clear();
		
		if (view instanceof TakenLeaveReportPanel) {
			@SuppressWarnings("unchecked")
			Set<LeaveTypeEnum> selectedValues = (Set<LeaveTypeEnum>) event.getProperty().getValue();
			List<LeaveTypeEnum> leaveTypes = new ArrayList<LeaveTypeEnum>(selectedValues);
			
			criteria.put(CRITERIA_LEAVE_TYPES, leaveTypes);
		}
		
	}
}
